package Testhandle_db;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

import java.sql.ResultSet;
import java.sql.SQLException;

import handle_db.DB_Connector;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class DB_ConnectorTest {

		@BeforeClass
		public static void setUpBeforeClass() throws Exception {
			System.out.println("Before class");
		}

		@AfterClass
		public static void tearDownAfterClass() throws Exception {
			System.out.println("After class");
		}

		@Before
		public void setUp() throws Exception {
			System.out.println("Before");
		}
		@Test
		public void testExecuteQuery(){
			String name=null;
			DB_Connector db=new DB_Connector();
			db.executeUpdate("insert into user_base_info values(1,0,'jack',1)");
			try {
				ResultSet z=db.executeQuery("select user_name from user_base_info");

				for(int i=0;;i++)
				{ 
            	
					if (z.next()) {
					
					name= z.getString("user_name");
					
					}
					else break;
				} 
			}
			catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			assertThat(name,is("jack"));
			//db.close();
		}
		@Test
		public void testExecuteUpdate(){
			DB_Connector db=new DB_Connector();
			int z=db.executeUpdate("delete from user_base_info where user_id=1");
			assertThat(z,is(1));
			db.close();
		}
		@After
		public void tearDown() throws Exception {
			System.out.println("After");
		}

}
